$(function () {
    $("#jqGrid").Grid({
        url: '../invite/list',
        colModel: [{
            label: 'id', name: 'id', index: 'id', key: true, hidden: true
        }, {
            label: '所属商家', name: 'deptName', index: 'deptName', width: 60
        }, {
            label: '邀请码', name: 'invitation', index: 'invitation', width: 60
        }, {
            label: '客户等级', name: 'grade', index: 'grade', width: 60, formatter: function(value) {
                return (value == 1 ? "客户" : "高级客户");
            }
        }, {
            label: '手机号', name: 'mobile', index: 'mobile', width: 60
        }, {
            label: '创建时间', name: 'createTime', index: 'createTime', width: 60, formatter: function (value) {
                return transDate(value);
            }
        }]
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        invite: {
            grade: 1
        },
        ruleValidate: {
            invitation: [
                {required: true, message: '邀请码不能为空', trigger: 'blur'}
            ]
        },
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            var str = "";
            for(var i = 0; i < 6; i++){
                var j = parseInt(Math.random() * 10).toString();
                str += j;
            }
            vm.invite = {invitation: str,grade: '1'};
        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            var url = vm.invite.id == null ? "../invite/save" : "../invite/update";

            Ajax.request({
                type: "POST",
                url: url,
                contentType: "application/json",
                params: JSON.stringify(vm.invite),
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        saveOrUpdateAndNext: function (event) {
            var url = vm.invite.id == null ? "../invite/save" : "../invite/update";

            Ajax.request({
                type: "POST",
                url: url,
                contentType: "application/json",
                params: JSON.stringify(vm.invite),
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        //vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    type: "POST",
                    url: "../invite/delete",
                    contentType: "application/json",
                    params: JSON.stringify(id),
                    successCallback: function (r) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });

            });
        },
        handleSubmitAndNext: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdateAndNext()
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../invite/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.invite = r.invite;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                page: page,
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
    }
});